package com.hui.controller;

import com.hui.model.Admin;
import com.hui.model.People;
import com.hui.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * TODO: 对象数据绑定
 *
 * @author chenth
 * @create 2018-07-23 23:10
 **/
@Controller
public class ObjectController {
    /**
     * 单一对象数据绑定
     * @param user
     * @return
     * http://localhost:8080/getUser.do?name=Tom&age=12
     */
    @RequestMapping("/getUser.do")
    @ResponseBody
    public String getObject(User user){
        return user.toString();
    }

    /**
     * 多层级数据对象绑定（People有User的对象）
     * @param people
     * @return
     * http://localhost:8080/getPeople.do?sex=man&user.name=Tom&user.age=12
     */
    @RequestMapping("/getPeople.do")
    @ResponseBody
    public String getPeople(People people){
        return people.toString();
    }

    /**
     * 同对象数据绑定
     * @param user
     * @param admin
     * @return
     * http://localhost:8080/getObjects.do?user.name=Tom&age=12&admin.name=Mike
     */
    @RequestMapping("/getObjects.do")
    @ResponseBody
    public String getObjects(User user, Admin admin){
        return user.toString() + " " + admin.toString();
    }

    /**
     * 为了区分同对象的数据绑定，加入@InitBinder注解来标识
     * @param webDataBinder

    @InitBinder("user")
    public void initUser(WebDataBinder webDataBinder){
        webDataBinder.setFieldDefaultPrefix("user.");
    }
    @InitBinder("admin")
    public void initAdmin(WebDataBinder webDataBinder){
        webDataBinder.setFieldDefaultPrefix("admin.");
    }*/
}
